package com.hk.crm.workbench.service.impl;

import com.hk.crm.VO.PaginationVO;
import com.hk.crm.settings.dao.UserDao;
import com.hk.crm.settings.domain.User;
import com.hk.crm.utils.SqlSessionUtil;
import com.hk.crm.workbench.dao.ActivityDao;
import com.hk.crm.workbench.dao.ActivityRemarkDao;
import com.hk.crm.workbench.domain.Activity;
import com.hk.crm.workbench.domain.ActivityRemark;
import com.hk.crm.workbench.service.ActivityService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActivityServiceImpl implements ActivityService {
    private ActivityDao proxydao= SqlSessionUtil.getSqlSession().getMapper(ActivityDao.class);
    private ActivityRemarkDao proxyremarkdao=SqlSessionUtil.getSqlSession().getMapper(ActivityRemarkDao.class);
    private UserDao proxyUserDao=SqlSessionUtil.getSqlSession().getMapper(UserDao.class);

    @Override
    public boolean save(Activity activity) {
        boolean flag=true;
        int count=proxydao.save(activity);
        if (count!=1){
            flag=false;
        }
        return flag;
    }

    @Override
    public PaginationVO<Activity> pageList(Map<String, Object> map) {
//        取得total
        int total=proxydao.getTotalByCondition(map);
//        取得dataList
        List<Activity> dataList= proxydao.getActivityListByCondition(map);
//        将total和dataList封装到VO中
        PaginationVO<Activity> vo=new PaginationVO<>();
        vo.setTotal(total);
        vo.setDatalist(dataList);
//        将VO返回
        return vo;
    }

    @Override
    public boolean delete(String[] ids) {
        boolean flag=true;
//        查询出需要删除的备注的数量
        int count=proxyremarkdao.getCountByAids(ids);
//        删除备注，返回收到影响的条数（实际删除数量）
        int relCount=proxyremarkdao.deleteByAids(ids);
        if (count!=relCount){//代表删除数据有误
            flag=false;
        }
//        删除市场活动
        int count3=proxydao.deleteByAids(ids);
        if (count3!= ids.length){
            flag=false;
        }
        return flag;
    }

    @Override
    public Map<String, Object> getUserListAndActivity(String id) {
//        取ulist
        List<User> userList=proxyUserDao.getUserList();
//        取市场活动对象a
        Activity a=proxydao.getById(id);
//        将两个打包成map
        Map<String,Object> map=new HashMap<>();
        map.put("ulist", userList);
        map.put("a", a);
//        返回map
        return map;
    }

    @Override
    public boolean update(Activity activity) {
        boolean flag=true;
        int count=proxydao.update(activity);
        if (count!=1){
            flag=false;
        }
        return flag;
    }

    @Override
    public Activity detail(String id) {
        Activity a=proxydao.detail(id);
        return a;
    }

    @Override
    public List<ActivityRemark> getRemarkListByAid(String activityId) {
        List<ActivityRemark> arlist=proxyremarkdao.getRemarkByAid(activityId);
        return arlist;
    }

    @Override
    public boolean deleteRemark(String id) {
        boolean flag=true;
        int count=proxyremarkdao.deleteById(id);
        if (count!=1){
            flag=false;
        }
        return flag;
    }

    @Override
    public boolean saveRemark(ActivityRemark ar) {
        boolean flag=true;
         int count=proxyremarkdao.saveRemark(ar);
         if (count!=1){
             flag=false;
         }
         return flag;
    }

    @Override
    public boolean updateRemark(ActivityRemark ar) {
        boolean flag=true;
        int count=proxyremarkdao.updateRemark(ar);
        if (count!=1){
            flag=false;
        }
        return flag;
    }

    @Override
    public List<Activity> getActivityListByClueId(String clueId) {
       List<Activity> activityList= proxydao.getActivityListByClueId(clueId);
       return activityList;
    }
}
